﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Lamex.mPPC.Common
{
    public class LogicHelper
    {
        /// <summary>
        /// 生产csv逻辑
        /// </summary>
        /// <param name="list">勾选的生产订单</param>
        /// <param name="list1">勾选的跟进表号</param>
        /// <param name="filename">文件名</param>
        /// <param name="size">最大开料尺寸</param>
        /// <returns></returns>
        public static DataTable DataImport_CSV(List<string> list, List<string> list1, string filename, string size)
        {
            DataTable table = Common.GetTable.GetTable_CSV();
            var helper = new SerialNoHelper();
            string stringNo = Common.GetDataTable.GetSerialNo();
            string strDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            //生成需要导出的dataTable
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder stringBuilder1 = new StringBuilder();
            stringBuilder.AppendFormat(" AND PRODUCT_ORDER_NO IN ('{0}')", string.Join("','", list.ToArray()));
            stringBuilder1.AppendFormat(" AND FOLLOW_NO IN ('{0}')", string.Join("','", list1.ToArray()));

            DataTable data = Common.GetDataTable.GetCSV_Detail(stringBuilder.ToString() + stringBuilder1.ToString(), size);
            if (data.Rows.Count > 0)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    //判断材料编号是否包含中文
                    if (IsChineseChart(data.Rows[i]["MES_DECOR"].ToString()))
                        continue;
                    double order_qty = Convert.ToDouble(data.Rows[i]["PRODUCT_ORDER_QTY"].ToString());
                    double cutting_qty = Convert.ToDouble(data.Rows[i]["CUTTING_QTY"].ToString());
                    double sums = order_qty * cutting_qty;
                    for (int j = 0; j < sums; j++)
                    {
                        DataRow dr = table.NewRow();
                        dr["BARCODE"] = helper.Generate(stringNo); //流水号
                        dr["MATERIAL"] = data.Rows[i]["MES_DECOR"].ToString();//材料编号
                        dr["LENGTH"] = data.Rows[i]["SNG_CUT_SIZE_X"].ToString();//单开_X
                        dr["WIDTH"] = data.Rows[i]["SNG_CUT_SIZE_Y"].ToString();//单开_Y
                        dr["THICKNESS"] = data.Rows[i]["THICKNESS"].ToString();//厚度
                        dr["GRAIN"] = data.Rows[i]["GRAIN"].ToString() == "X" ? "0" : "1";//木纹方向
                        dr["QUANTITY"] = "1";//默认值1
                        dr["TYPE"] = "9";//默认值9
                        dr["Finish_Length"] = data.Rows[i]["SNG_CUT_SIZE_X"].ToString();//A面布色
                        dr["Finish_Width"] = data.Rows[i]["SNG_CUT_SIZE_Y"].ToString();//B面布色
                        dr["PART_TYPE"] = data.Rows[i]["PART_TYPE"].ToString();//材料类型
                        dr["PART_NO"] = data.Rows[i]["PART_NO"].ToString();//部件编号
                        dr["PART_DESC"] = data.Rows[i]["PART_DESC"].ToString();//部件描述
                        dr["MODEL_NO"] = data.Rows[i]["MODEL_NO"].ToString();//型号
                        dr["PRODUCT_DATE"] = data.Rows[i]["PRODUCT_DATE"].ToString();//生产日期
                        dr["CUTTING_TYPE"] = data.Rows[i]["CUTTING_TYPE"].ToString();//开料类型
                        dr["MACHINE_SAW"] = "HP430";//机器名 默认值HP430
                        dr["GRID_POSITION"] = data.Rows[i]["GRID_POSITION"].ToString();//ECN编号
                        dr["FINISH_DATE"] = data.Rows[i]["FINISH_DATE"].ToString();//完成日期
                        dr["FACE_COLOR_B"] = data.Rows[i]["FACE_COLOR_A"].ToString();//销售订单
                        dr["FACE_COLOR_B"] = data.Rows[i]["FACE_COLOR_B"].ToString();//行号
                        dr["FOLLOW_NO"] = data.Rows[i]["FOLLOW_NO"].ToString();//跟进表编号
                        dr["DWG_NO"] = data.Rows[i]["DWG_NO"].ToString();//图纸号
                        dr["PRODUCT_ORDER_NO"] = data.Rows[i]["PRODUCT_ORDER_NO"].ToString();//生产订单
                        dr["TRANSFER_BATCH_NO"] = data.Rows[i]["TRANSFER_BATCH_NO"].ToString();//传输批次
                        dr["ORDER_QTY"] = sums;//总开料数量
                        dr["PRIORITY"] = "3";
                        dr["INFO_20"] = filename; //文件名 
                        dr["UPDATE_TS"] = strDate;
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                        table.Rows.Add(dr);
                    }
                }
            }
            return table;
        }
        //判断是否包含汉字
        public static bool IsChineseChart(string strMarterNo)
        {
            Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正则表达式
            if (reg.IsMatch(strMarterNo))
                return true;
            else
                return false;
        }

        public static DataTable Data_CuttingState(DataTable dt, string filename)
        {
            DataTable dt_CuttingState = Common.GetTable.GetTable_CuttingState();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt_CuttingState.NewRow();
                dr["Optimization_No"] = filename.Trim();
                dr["PRODUCT_ORDER_NO"] = dt.Rows[i]["PRODUCT_ORDER_NO"].ToString();
                dr["FOLLOW_NO"] = dt.Rows[i]["FOLLOW_NO"].ToString();
                dr["doneCSV"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                dr["doneCSV_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                dt_CuttingState.Rows.Add(dr);
            }
            return dt_CuttingState;
        }
        /// <summary>
        /// 【显示待转HPL430列表】
        /// </summary>
        /// <returns></returns>
        public static DataTable GetOptionName(List<string> list)
        {
            DataTable dataTable = Common.GetTable.GetTable_Opt();
            foreach (string OptNo in list)
            {
                DataRow dr = dataTable.NewRow();
                dr["优化单号"] = OptNo;
                dataTable.Rows.Add(dr);
            }
            return dataTable;
        }
        /// <summary>
        /// 【显示待转PTX列表】
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable GetGridData_PTX(List<string> list, DirectoryInfo directoryInfo)
        {
            DataTable dataTable = Common.GetTable.GetTable_PTX();//生成Table框架
            DataTable data = Common.GetDataTable.GetOpt_State();//获取只存在csv生成时间的优化单号
            //正常生成CSV显示状态                                                 
            foreach (string OptNo in list)
            {
                DataRow dr = dataTable.NewRow();
                dr["优化单号"] = OptNo;
                dr["优化状态"] = "CSV已生成";
                dataTable.Rows.Add(dr);
            }
            //HPO优化后显示状态
            if (data.Rows.Count > 0)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    //定义文件是否被手工删除
                    bool flag = true;
                    //获取该目录下所有子文件夹
                    foreach (DirectoryInfo NextFolder in directoryInfo.GetDirectories())
                    {
                        //获取子文件夹所有csv文件
                        foreach (FileInfo f in NextFolder.GetFiles())
                        {
                            if (f.Name.Contains(data.Rows[i]["Optimization_No"].ToString().Trim()))
                            {
                                DataRow dr = dataTable.NewRow();
                                dr["优化单号"] = data.Rows[i]["Optimization_No"].ToString().Trim();
                                dr["优化状态"] = "HPO已读入";
                                dataTable.Rows.Add(dr);
                                flag = false;
                                continue;
                            }

                        }
                    }
                    if (flag && !list.Contains(data.Rows[i]["Optimization_No"].ToString().Trim()))
                    {
                        DataRow dr = dataTable.NewRow();
                        dr["优化单号"] = data.Rows[i]["Optimization_No"].ToString().Trim();
                        dr["优化状态"] = "CSV异常";
                        dataTable.Rows.Add(dr);
                    }
                }
            }
            return dataTable;
        }

        /// <summary>
        /// 更新donePTX字段
        /// </summary>
        /// <param name="Optimization_No"></param>
        /// <param name="donePTX"></param>
        /// <returns></returns>
        public static bool Update_Cutting_State_PTX(string Optimization_No, string donePTX)
        {
            string sql_update = @"update [LamexMESManager].[dbo].[PPC_HPL430_Cutting_State] 
            set donePTX='" + donePTX + "',doneCSV_USER='" + Lamex.Common.Loginer.CurrentUser.AccountName + "' where Optimization_No='" + Optimization_No + "'";
            int n = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql_update);
            if (n > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新doneTransfer字段
        /// </summary>
        /// <param name="Optimization_No"></param>
        /// <param name="doneTransfer"></param>
        /// <returns></returns>
        public static bool Update_Cutting_State_Tranfer(string Optimization_No, string doneTransfer)
        {
            string sql_update = @"update [LamexMESManager].[dbo].[PPC_HPL430_Cutting_State] 
            set doneTransfer='" + doneTransfer + "',doneTransfer_USER='" + Lamex.Common.Loginer.CurrentUser.AccountName + "' where Optimization_No='" + Optimization_No + "'";
            int n = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql_update);
            if (n > 0)
                return true;
            else
                return false;
        }



    }
}
